<template>
	<view class="container">
		<view class="address" v-for="(item, index) in list" :key="item.id">
			<view class="address-info" @click="confirmAddress(item)">
				<view class="address-info-top">
					<text class="tag" v-if="item.status === 1">默认</text>
					<text class="name">{{item.nickname}}</text>
					<text class="phone">{{item.phone}}</text>
				</view>
				<view class="address-info-bottom">
					{{item.address}}
				</view>
			</view>
			<view class="edit-icon">
        <image style="width: 32rpx;height: 32rpx;margin-bottom: 12rpx;margin-right: 12rpx" src="/static/icon/del.png" @click="delAddress(item)"></image>
        <image src="/static/icon/edit.png" @click="editAddress(item)"></image>
			</view>
		</view>
		<view class="btn">
			<button @click="add()">添加新地址</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: []
			}
		},
    onShow() {
      this.$api.getAddressList({
        pageNo: 1,
        pageSize: 999
      }).then(res => {
        this.list = res.rows
      })
    },
		methods: {
			add() {
				uni.navigateTo({
					url: "/pages/address/add"
				})
			},
      editAddress(item) {
        uni.navigateTo({
          url: "/pages/address/add?id=" + item.id
        })
      },
      confirmAddress(item) {
        uni.setStorageSync('defaultAddress', item)
        uni.navigateBack()
      },
      delAddress(item) {
        uni.showModal({
          title: '提示',
          content: '是否删除该地址?',
          success: res => {
            if (res.confirm) {
              this.$api.deleteAddress(item.id).then(res => {
                this.$utils.toastTip("删除成功！")
                this.$api.getAddressList({
                  pageNo: 1,
                  pageSize: 999
                }).then(res => {
                  this.list = res.rows
                })
              })
            }
          }
        })
      }
		}
	}
</script>

<style>
	page {
		background: #F8F9FA;
	}
</style>

<style lang="scss" scoped>
	.container {
		margin: 20rpx 30rpx;
	}

	.address {
		display: flex;
		align-items: center;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 32rpx 20rpx;
		margin-bottom: 22rpx;

		.address-info {
      flex-grow: 1;
			display: flex;
			flex-direction: column;
			padding: 0rpx 40rpx 0rpx 20rpx;

			.address-info-top {
				font-size: 30rpx;
				font-weight: 600;
				color: #333333;

				.tag {
					background: #D5EBD3;
					border-radius: 10rpx;
					font-size: 16rpx;
					color: #f56c6c;
					padding: 4rpx 8rpx;
					margin-right: 16rpx;
				}

				.name {
					margin-right: 16rpx;
				}
			}

			.address-info-bottom {
				font-size: 26rpx;
				color: #999999;
				margin-top: 10rpx;
			}
		}

		.edit-icon {

			image {
				width: 52rpx;
				height: 52rpx;
			}
		}
	}

	.btn {
		margin-top: 70rpx;
		width: 100%;

		button {
			flex: 1;
			border-radius: 46rpx;
			background: #f56c6c;
			color: #FFFFFF;
			font-size: 30rpx;
			padding: 12rpx 0rpx;
		}
	}
</style>
